import * as step from '@flow-step/step-toolkit'
import process from 'process'
import {SaeInterface} from './sae'
import {ConfirmPipelineBatchRequest, ConfirmPipelineBatchResponse} from '@alicloud/sae20190506'
import {IParams} from './params'

export async function confirm(params:IParams, sae: SaeInterface) {
    if(!process.env.NEXT_PIPELINE_ID){
        throw new Error('NEXT_PIPELINE_ID is not set in environment variables')
    }
    step.info(`CONTINUE DEPLOYMENT, changeOrderId: ${process.env.NEXT_PIPELINE_ID} `)

    const request = new ConfirmPipelineBatchRequest({
        pipelineId: process.env.NEXT_PIPELINE_ID,
        confirm: true,
    })
    let response: ConfirmPipelineBatchResponse
    try {
        response = await sae.confirmPipelineBatch(request)
    }catch (error){
        throw new Error(`failed to confirmPipelineBatch: ${error}`)
    }
    if (response.body?.code != '200'){
        throw new Error(`failed to continue deployment, code: ${response.body?.code}, message: ${response.body?.message}`)
    }

    step.interaction.runAction('CHECK')
    step.addOutput(`CHANGE_ORDER_ID=${params.CHANGE_ORDER_ID}`)
    step.addOutput(`APP_NAME=${params.APP_NAME}`)
    step.interaction.addLink(params.domestic?"发布单详情":"Change Order Details", `https://saenext.console.aliyun.com/${params.regionID}/app-list/${params.APPLICATION_ID}/micro-app/record/${params.CHANGE_ORDER_ID}`,41)

}